package com.qdhh.enrollment.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qdhh.enrollment.common.BusinessException;
import com.qdhh.enrollment.mapper.QuizQuestionMapper;
import com.qdhh.enrollment.model.QuizQuestion;
import com.qdhh.enrollment.service.AdminQuestionService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 管理员题目服务实现类
 */
@Service
public class AdminQuestionServiceImpl extends ServiceImpl<QuizQuestionMapper, QuizQuestion> implements AdminQuestionService {

    @Override
    public Page<QuizQuestion> listQuestions(int page, int size, String category, String type) {
        Page<QuizQuestion> pageObj = new Page<>(page, size);
        LambdaQueryWrapper<QuizQuestion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(QuizQuestion::getDeleted, 0);
        
        if (StringUtils.hasText(category)) {
            queryWrapper.eq(QuizQuestion::getCategory, category);
        }
        
        if (StringUtils.hasText(type)) {
            queryWrapper.eq(QuizQuestion::getType, type);
        }
        
        queryWrapper.orderByDesc(QuizQuestion::getCreatedAt);
        return page(pageObj, queryWrapper);
    }

    @Override
    public QuizQuestion getQuestionById(String id) {
        QuizQuestion question = getById(id);
        if (question == null || (question.getDeleted() != null && question.getDeleted() == 1)) {
            throw new BusinessException("题目不存在");
        }
        return question;
    }

    @Override
    public QuizQuestion createQuestion(QuizQuestion question) {
        if (question == null || !StringUtils.hasText(question.getQuestion())) {
            throw new BusinessException("题目内容不能为空");
        }

        LocalDateTime now = LocalDateTime.now();
        if (!StringUtils.hasText(question.getId())) {
            question.setId(IdUtil.simpleUUID());
        }
        question.setDeleted(0);
        question.setVersion(1);
        if (question.getEnabled() == null) {
            question.setEnabled(1);
        }
        if (question.getOrderNum() == null) {
            question.setOrderNum(0);
        }
        question.setCreatedAt(now);
        question.setUpdatedAt(now);

        save(question);
        return question;
    }

    @Override
    public QuizQuestion updateQuestion(String id, QuizQuestion question) {
        QuizQuestion existing = getQuestionById(id);

        if (StringUtils.hasText(question.getQuestion())) {
            existing.setQuestion(question.getQuestion());
        }
        if (question.getOptions() != null) {
            existing.setOptions(question.getOptions());
        }
        if (StringUtils.hasText(question.getAnswer())) {
            existing.setAnswer(question.getAnswer());
        }
        if (StringUtils.hasText(question.getType())) {
            existing.setType(question.getType());
        }
        if (StringUtils.hasText(question.getCategory())) {
            existing.setCategory(question.getCategory());
        }
        if (question.getEnabled() != null) {
            existing.setEnabled(question.getEnabled());
        }
        if (question.getOrderNum() != null) {
            existing.setOrderNum(question.getOrderNum());
        }
        if (question.getScore() != null) {
            existing.setScore(question.getScore());
        }
        if (StringUtils.hasText(question.getTags())) {
            existing.setTags(question.getTags());
        }

        existing.setUpdatedAt(LocalDateTime.now());
        updateById(existing);
        return existing;
    }

    @Override
    public void deleteQuestion(String id) {
        QuizQuestion question = getQuestionById(id);
        question.setDeleted(1);
        question.setUpdatedAt(LocalDateTime.now());
        updateById(question);
    }

    @Override
    public void batchDeleteQuestions(List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new BusinessException("删除ID列表不能为空");
        }
        
        List<QuizQuestion> questions = listByIds(ids);
        LocalDateTime now = LocalDateTime.now();
        for (QuizQuestion question : questions) {
            question.setDeleted(1);
            question.setUpdatedAt(now);
        }
        updateBatchById(questions);
    }
}

